package com.android.infantschool.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.bean.BaseBean;
import com.android.infantschool.bean.ResponseBean;
import com.android.infantschool.model.config.AppConfig;
import com.android.infantschool.model.mvp.presenter.AddRemarkPresenter;
import com.android.infantschool.model.mvp.presenter.AddRemarkPresenterImpl;
import com.android.infantschool.model.mvp.view.AddRemarkView;
import com.android.infantschool.util.EdittorUtil;
import com.android.infantschool.util.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Administrator
 * @particulars
 * @time 2019\7\5 0005 15:52
 * @class describe
 */
public class NewBabyRemarkActivity extends BaseActivity implements RatingBar.OnRatingBarChangeListener, AddRemarkView {

    @BindView(R.id.img_back)
    public ImageView imgBack;
    @BindView(R.id.title)
    public RelativeLayout rlTitle;
    @BindView(R.id.tv_title)
    public TextView tvTitle;
    @BindView(R.id.ed_content)
    public EditText edContent;
    @BindView(R.id.tv_content_size)
    public TextView tvContentSize;
    @BindView(R.id.btn_release)
    public Button btnRelease;
    @BindView(R.id.ed_baby_name)
    public EditText edBabyName;
    @BindView(R.id.rating_bar_1)
    public RatingBar ratingBar1;
    @BindView(R.id.rating_bar_2)
    public RatingBar ratingBar2;
    @BindView(R.id.rating_bar_3)
    public RatingBar ratingBar3;
    @BindView(R.id.rating_bar_4)
    public RatingBar ratingBar4;
    @BindView(R.id.rating_bar_5)
    public RatingBar ratingBar5;
    @BindView(R.id.rating_bar_6)
    public RatingBar ratingBar6;
    @BindView(R.id.rating_bar_7)
    public RatingBar ratingBar7;
    @BindView(R.id.rating_bar_8)
    public RatingBar ratingBar8;
    @BindView(R.id.rating_bar_9)
    public RatingBar ratingBar9;
    @BindView(R.id.rating_bar_10)
    public RatingBar ratingBar10;
    @BindView(R.id.rating_bar_11)
    public RatingBar ratingBar11;

    public String babyName = "";

    private AddRemarkPresenter remarkPresenter = null;

    private String[] strings = new String[]{"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"};//存放评语星星等级  默认0

    public static Intent newIntent(Activity activity, String babyName, String babyId) {
        Intent intent = new Intent(activity, NewBabyRemarkActivity.class);
        intent.putExtra("babyName", babyName);
        intent.putExtra("babyId", babyId);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_baby_remark);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        remarkPresenter = new AddRemarkPresenterImpl(this);
        rlTitle.setPadding(0, this.height, 0, 0);
        tvTitle.setText("新建评语");
        edBabyName.setText(getIntent().getStringExtra("babyName"));
        edBabyName.setEnabled(false);
        edBabyName.setKeyListener(null);
        EdittorUtil.hideInput(this);
        ratingBar1.setOnRatingBarChangeListener(this);
        ratingBar2.setOnRatingBarChangeListener(this);
        ratingBar3.setOnRatingBarChangeListener(this);
        ratingBar4.setOnRatingBarChangeListener(this);
        ratingBar5.setOnRatingBarChangeListener(this);
        ratingBar6.setOnRatingBarChangeListener(this);
        ratingBar7.setOnRatingBarChangeListener(this);
        ratingBar8.setOnRatingBarChangeListener(this);
        ratingBar9.setOnRatingBarChangeListener(this);
        ratingBar10.setOnRatingBarChangeListener(this);
        ratingBar11.setOnRatingBarChangeListener(this);
        edContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int length = s.toString().length();
                length = 500 - length;
                tvContentSize.setText(length + "/500");
            }
        });
    }

    @OnClick({R.id.btn_release, R.id.img_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_release:
                String context = edContent.getText().toString().trim();
                if (TextUtils.isEmpty(context)) {
                    ToastUtil.showShortToast(this, "请输入评语内容");
                    return;
                }
                Map<String, String> map = new HashMap<>();
                map.put("token", AppConfig.getToken());
                map.put("content", context);
                map.put("babyId", getIntent().getStringExtra("babyId"));
                map.put("selfReliance", strings[0]);//自立
                map.put("politeness", strings[1]);//礼貌
                map.put("emotion", strings[2]);//情绪
                map.put("contact", strings[3]);//人际交往
                map.put("hygiene", strings[4]);//个人卫生
                map.put("dining", strings[5]);//进餐习惯
                map.put("sleep", strings[6]);//睡觉习惯
                map.put("collective", strings[7]);//集体生活
                map.put("reading", strings[8]);//阅读能力
                map.put("logica", strings[9]);//逻辑思维能力
                map.put("sports", strings[10]);//体育锻炼
                Log.e(TAG, "map:" + map.toString());
                remarkPresenter.postAddRemark(map);
                this.showLoading();
                break;
            case R.id.img_back:
                this.finish();
                break;
        }
    }

    @Override
    public void errorAddRemark(String error) {
        this.dismissLoading();
        ToastUtil.showShortToast(this, "网络异常:" + error);
    }

    @Override
    public void successAddRemark(BaseBean baseBean) {
        this.dismissLoading();
        ResponseBean remarkBean = (ResponseBean) baseBean;
        if (remarkBean.getCode() == 0) {
            ToastUtil.showShortToast(this, "提交成功");
            this.finish();
        } else {
            ToastUtil.showShortToast(this, "提交失败:" + remarkBean.getMsg());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
        switch (ratingBar.getId()) {
            case R.id.rating_bar_1:
                Log.e(TAG, "星星:" + rating);
                strings[0] = String.valueOf((int) rating);
                break;
            case R.id.rating_bar_2:
                Log.e(TAG, "星星:" + rating);
                strings[1] = String.valueOf((int) rating);
                break;
            case R.id.rating_bar_3:
                Log.e(TAG, "星星:" + rating);
                strings[2] = String.valueOf((int) rating);
                break;
            case R.id.rating_bar_4:
                Log.e(TAG, "星星:" + rating);
                strings[3] = String.valueOf((int) rating);
                break;
            case R.id.rating_bar_5:
                Log.e(TAG, "星星:" + rating);
                strings[4] = String.valueOf((int) rating);
                break;
            case R.id.rating_bar_6:
                Log.e(TAG, "星星:" + rating);
                strings[5] = String.valueOf((int) rating);
                break;
            case R.id.rating_bar_7:
                Log.e(TAG, "星星:" + rating);
                strings[6] = String.valueOf((int) rating);
                break;
            case R.id.rating_bar_8:
                Log.e(TAG, "星星:" + rating);
                strings[7] = String.valueOf((int) rating);
                break;
            case R.id.rating_bar_9:
                Log.e(TAG, "星星:" + rating);
                strings[8] = String.valueOf((int) rating);
                break;
            case R.id.rating_bar_10:
                Log.e(TAG, "星星:" + rating);
                strings[9] = String.valueOf((int) rating);
                break;
            case R.id.rating_bar_11:
                Log.e(TAG, "星星:" + rating);
                strings[10] = String.valueOf((int) rating);
                break;
        }
    }

}